import 'dart:math';

import 'package:flutter/material.dart';
import 'package:xvideos/routers/jump_router.dart';
import 'package:xvideos/tools_base/global_store/local_ads_info_store.dart';

import '../../hj_model/splash/ads_model.dart';
import '../widget/net_image_widget.dart';
import 'ads_banner_widget.dart';

class AdsGridViewWidget extends StatelessWidget {
  final int position;
  final bool isBigStyle; // true 10宫格模式
  final bool isBannerStyle;
  final double? aspectRatio;
  final EdgeInsetsGeometry? padding;
  final List<AdsInfoModel>? adsArr;
  final bool isDarkWebStyle;
  final double horCount; // 一屏显示几个

  AdsGridViewWidget(
    this.position, {
    this.isBigStyle = false,
    this.isBannerStyle = false,
    this.padding,
    this.adsArr,
    this.aspectRatio,
    this.isDarkWebStyle = false,
    this.horCount = 5,
  });

  List<AdsInfoModel> get adsList => adsArr ?? getAdvByTypeSync(position);

  @override
  Widget build(BuildContext context) {
    if (adsList.isEmpty) return SizedBox();
    if (isBigStyle) {
      return Container(
        padding: padding,
        child: GridView.builder(
          padding: EdgeInsets.zero,
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 5,
            mainAxisSpacing: 12,
            crossAxisSpacing: 10,
            childAspectRatio: 60 / 82,
          ),
          itemCount: min(10, adsList.length),
          itemBuilder: (context, index) {
            AdsInfoModel model = adsList[index];
            return _buildAdItem(model);
          },
        ),
      );
    } else if (isBannerStyle) {
      return _buildBannerStyle();
    } else {
      return Container(
        margin: padding,
        child: LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            final width = (constraints.maxWidth - (horCount - 1) * 10) / horCount;
            final height = width * 82 / 60;
            return SizedBox(
              height: height,
              child: ListView.separated(
                padding: EdgeInsets.zero,
                itemCount: adsList.length,
                scrollDirection: Axis.horizontal,
                separatorBuilder: (context, index) => SizedBox(width: 10),
                itemBuilder: (context, index) {
                  AdsInfoModel model = adsList[index];
                  return Container(
                    width: width,
                    child: _buildAdItem(model),
                  );
                },
              ),
            );
          },
        ),
      );
    }
  }

  Widget _buildBannerStyle() {
    return Container(
      padding: padding,
      alignment: Alignment.center,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(8),
        child: AspectRatio(
          aspectRatio: aspectRatio ?? 720 / 150,
          child: AdsBannerWidget(
            adsList,
            isDarkWebStyle: isDarkWebStyle,
            onItemClick: (index) {
              var ad = adsList[index];
              pushToPageByLink(ad.href, id: ad.id);
            },
          ),
        ),
      ),
    );
  }

  Widget _buildAdItem(AdsInfoModel model) {
    return InkWell(
      onTap: () {
        pushToPageByLink(model.href, id: model.id);
      },
      child: Column(
        children: [
          AspectRatio(
            aspectRatio: 1,
            child: NetworkImageLoader(
              imageUrl: model.cover ?? "",
              borderRadius: 12,
            ),
          ),
          Spacer(),
          Text(
            '${model.title?.trim() ?? ""}',
            maxLines: 1,
            style: TextStyle(
              color: Colors.white,
              fontSize: 11,
            ),
          ),
          Spacer(),
        ],
      ),
    );
  }
}
